package com.cjh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cjh.pojo.Admin;
import com.cjh.pojo.User;
import com.cjh.service.AdminService;
import com.cjh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private UserService userService;

    @Autowired
    private AdminService adminService;

//    @PostMapping("/login")
//    public Object login(@RequestParam("username") String username,
//                        @RequestParam("password") String password,
//                        @RequestParam("status") String status){
//
//        Object result=null;
//        switch (status){
//            case "user":
//                QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
//                userQueryWrapper.eq("username",username).eq("password",password);
//                result = userService.getOne(userQueryWrapper);
//                break;
//            case "admin":
//                QueryWrapper<Admin> adminQueryWrapper=new QueryWrapper<>();
//                adminQueryWrapper.eq("username",username).eq("password",password);
//                result = adminService.getOne(adminQueryWrapper);
//                break;
//        }
//        return result;
//
//    }

    /**
     * shiro调用的login方法
     * @param username
     * @param status
     * @return
     */
    @PostMapping("/login/{status}")
    public Object login(@RequestParam("username") String username,
                        @PathVariable("status") String status){

        Object result=null;
        switch (status){
            case "user":
                QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
                userQueryWrapper.eq("username",username);
                result = userService.getOne(userQueryWrapper);
                break;
            case "admin":
                QueryWrapper<Admin> adminQueryWrapper=new QueryWrapper<>();
                adminQueryWrapper.eq("username",username);
                result = adminService.getOne(adminQueryWrapper);
                break;
        }
        return result;

    }

}
